package com.ndood.base.controller;

import com.ndood.base.pojo.Label;
import com.ndood.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 1.1.31 跨域处理
 */
@CrossOrigin
/**
 * 1.1.19 标签控制器类
 */
@RestController
@RequestMapping("/label")
public class LabelController {

	@Autowired
	private LabelService labelService;

	/**
	 * 1.1.20 查询全部列表
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Result findAll(){
		List<Label> list = labelService.findAll();
		return new Result(true, StatusCode.OK,"查询成功",list);
	}

	/**
	 * 1.1.21 根据ID查询
	 */
	@RequestMapping(value="/{id}",method = RequestMethod.GET)
	public Result findById(@PathVariable String id){
		System.out.println("No.2");

		Label label = labelService.findById(id);
		return new Result(true,StatusCode.OK,"查询成功",label);
	}

	/**
	 * 1.1.22 增加标签
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Result add(@RequestBody Label label){
		labelService.add(label);
		return new Result(true,StatusCode.OK,"添加成功");
	}

	/**
	 * 1.1.23 修改标签
	 */
	@RequestMapping(value="/{id}" , method = RequestMethod.PUT)
	public Result update(@RequestBody Label label, @PathVariable  String id){
		label.setId(id);
		labelService.update(label);
		return new Result(true,StatusCode.OK,"修改成功");
	}

	/**
	 * 1.1.24 删除标签
	 */
	@RequestMapping(value="/{id}",method = RequestMethod.DELETE)
	public Result deleteById(@PathVariable String id){
		labelService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}

	/**
	 * 1.2.3 条件查询
	 */
	@RequestMapping(value = "/search",method = RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap){
		List<Label> list = labelService.findSearch(searchMap);
		return new Result(true,StatusCode.OK,"查询成功",list);
	}

	/**
	 * 1.2.5 带分页条件查询
	 */
	@RequestMapping(value = "/search/{page}/{size}",method = RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap,@PathVariable int page,@PathVariable int size){
		Page<Label> pageList = labelService.findSearch(searchMap,page,size);
		//封装分页结果
		PageResult<Label> pageResult=new PageResult<Label>(pageList.getTotalElements(),pageList.getContent());
		return new Result(true,StatusCode.OK,"查询成功",pageResult);
	}

}
